/*-----------------------------------------------------------------------------------*/
/* Nuvoton Corporation confidential                                                  */
/*                                                                                   */
/* Copyright (c) 2009 by Nuvoton Corporation                                         */
/* All rights reserved                                                               */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/
/* File Contents:                                                                    */
/*   usbd_ram_diag.c                                                                 */
/*                                                                                   */
/* This file contains:                                                               */
/*                                                                                   */
/* Project:                                                                          */
/*                                                                                   */
/* Remark:                                                                           */
/*   1. In usbd.h, define TEST_RAM for RAM disk test.                                */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "wblib.h"
#include "NUC900_reg.h"
#include "NUC900_sic.h"
#include "NUC900_udc.h"
#include "ide.h"


#define uprintf		sysprintf
#define ugetchar	sysGetChar


//------------------------- Program -------------------------//
void usbdtestmsg (void)
{
	int ch;
	
	sysprintf("Description: USB RAM disk test. Plug the USB cable in PC.\n");
	sysprintf("Test   Tool: None.\n");
#if defined(NUC950)
	sysprintf("H/W Setting: Set JP3 at USBD.\n");
#elif defined(NUC951)
	sysprintf("H/W Setting: Set JP16 at USBD.\n");	
#else	
	sysprintf("H/W Setting: Set JP11 at USBD.\n");
#endif	
	sysprintf("Test Result: Copy any file to removable disk. And access it.\n");
	sysprintf("             If it's work, test is success.\n");
	
	sysprintf("\nAny key start test ...\n");
	ch = sysGetChar();	
}

void usbdExample (VOID)
{
	int ch;
	
	/* configure DRAM setting */
	DRAM_Identify(1);
	
	/* initialize USB Device function */
    udcInit();  
    
    /* Time-out mechanism */
    //sysSetTimerReferenceClock(TIMER0, 15000000);
	//sysStartTimer(TIMER0, 100, PERIODIC_MODE);
    
    usbdtestmsg();    
    sysprintf("\nStart test ...\n");
    sysprintf("Press any key to exit test ... \n");
    
   	while (1)
	{
		if (USBModeFlag)
			udcMassBulk();
			
		if (inpw(REG_UART0_LSR) & 0x01)	 /* get any key to exit test */
		{
			sysDisableInterrupt(IRQ_USBD);
			outpw(REG_PWRON, inpw(REG_PWRON) & (~0x400));  /* pull low the D+ */
			sysprintf("\n Exit test\n");
			break;
		}
	}
}

INT  diag_usbd(VOID)
{
	usbdExample();
	return 0;
}

